package com.xiaotao.publisher;

//import com.xiaotao.util.ConstantUtil;
import com.xiaotao.util.RabbitMessageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import static com.xiaotao.config.RabbitConfig.*;

@Component
@Slf4j
public class CustomPublisher {
    @Resource
    private
    RabbitMessageUtil rabbitMessageUtil;

    @Resource
    private
    RabbitTemplate rabbitTemplate;

    public void send(String msg, Integer timeout)
    {
        rabbitTemplate.setConfirmCallback(rabbitMessageUtil);
        rabbitTemplate.setReturnsCallback(rabbitMessageUtil);

        rabbitTemplate.convertAndSend(
                EXCHANGE_CHECK_ORDER_STATUS,
                QUEUE_CHECK_ORDER_STATUS,
                msg,
                message -> {message.getMessageProperties().setDelay(timeout);
                    return message;
                }
        );
        log.info("已发送延迟消息:{}, 延迟时间: {}ms", msg, timeout);
    }
}
